package com.douqu.game.core.entity.ext.data.challenge;

import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.entity.EntityObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Gavin.
 * Description:
 * Date: 2018/7/5 0005 下午 6:27
 * Huan Yu Copyright (c) 2017 All Rights Reserved.
 */
public class HegemonyResultItem extends EntityObject{

    private Integer type;

    private List<HegemonyResultObject> objects;

    private int addition;

    private int rate;

    public HegemonyResultItem(){
        objects = new ArrayList<>();
    }

    public void loadFrom(ByteBuffer buffer)
    {
        type = buffer.readByte();
        int temp = buffer.readByte();
        int size = 0;
        HegemonyResultObject obj = null;
        if(temp < 0)
        {
            //存储的值超过byte的取值范围时会读取成负值,这里逆推成int
            size = Byte.MAX_VALUE + 1 + Math.abs(Byte.MIN_VALUE - temp);
        }
        else
            size = temp;

        for (int i = 0; i < size; i++){
            obj = new HegemonyResultObject();
            obj.loadFrom(buffer);
            objects.add(obj);
        }
        addition = buffer.readInt();
        rate = buffer.readByte();
    }

    public void writeTo(ByteBuffer buffer)
    {
        buffer.writeByte(type);
        buffer.writeByte(objects.size());
        for (HegemonyResultObject obj:objects){
            obj.writeTo(buffer);
        }
        buffer.writeInt(addition);
        buffer.writeByte(rate);
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public List<HegemonyResultObject> getObjects() {
        if (objects == null){
            objects = new ArrayList<>();
        }
        return objects;
    }

    public void setObjects(List<HegemonyResultObject> objects) {
        this.objects = objects;
    }

    public int getAddition() {
        return addition;
    }

    public void setAddition(int addition) {
        this.addition = addition;
    }

    public void addAddition(int value){
        this.addition += addition;
    }

    public int getRate() {
        return rate;
    }

    public void setRate(int rate) {
        this.rate = rate;
    }

    public void addObjects(HegemonyResultObject object){
        if(objects == null){
            objects = new ArrayList<>();
        }
        objects.add(object);
    }

}
